package com.yxhl.zoume.core.user.presenter.passenger;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;

import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.presenter.UpPullPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.common.ui.view.UpPullRefreshView;
import com.yxhl.zoume.core.user.view.passenger.PassengerMgmtView;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.data.http.rest.param.passenger.GetPassengersParam;
import com.yxhl.zoume.data.http.rest.param.passenger.PageOptions;
import com.yxhl.zoume.data.http.rest.response.passenger.GetPassengersResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.passenger.GetPassengersUseCase;
import com.yxhl.zoume.utils.LOG;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/3/24.
 */
public class PassengerMgmtPresenter extends UpPullPresenter implements IPresenter {

    public static final int PAGE_SIZE = 10;
    private static final String TAG = "PassengerMPresenter";
    @Inject
    GetPassengersUseCase mGetPassengersUseCase;
    private PassengerMgmtView mPassengerMgmtView;
    private UpPullRefreshView<ZMPassenger> mUpPullRefreshView;


    @Inject
    public PassengerMgmtPresenter(GetPassengersUseCase passengersUseCase, Context context) {
        super(context);
        this.mGetPassengersUseCase = passengersUseCase;
    }

    /**
     * 加载网络数据
     */

    @Override
    protected void loadDataForDownRefresh() {

        GetPassengersParam passengersParam = new GetPassengersParam(
                getMobile(),
                getToken(),
                new PageOptions(getCurrentPage(), PAGE_SIZE));
        mGetPassengersUseCase.execute(passengersParam,new ZMSubscriber<GetPassengersResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mPassengerMgmtView.closeRefreshAnimation();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mPassengerMgmtView.closeRefreshAnimation();
                    }

                    @Override
                    public void onNext(GetPassengersResponse getPassengersResponse) {
                        super.onNext(getPassengersResponse);
                        if (getPassengersResponse != null) {
                            boolean succ = getPassengersResponse.isSucc();
                            if (succ) {
                                List<ZMPassenger> passengerList = getPassengersResponse.getAddedPassengers();
                                if (passengerList != null && passengerList.size() > 0) {
                                    mPassengerMgmtView.renderDataOnSuccess(passengerList);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    protected void loadDataForUpPull() {
        LOG.e(TAG, "CurrentPage=" + getCurrentPage());
        GetPassengersParam passengersParam = new GetPassengersParam(
                getMobile(),
                getToken(),
                new PageOptions(getCurrentPage(), PAGE_SIZE));
        mGetPassengersUseCase.execute(passengersParam,new ZMSubscriber<GetPassengersResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        LOG.e(TAG, "上拉加载网络请求完成");
                        mUpPullRefreshView.closeUpPullRefresh();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        upPullRefreshFailure();
                        mUpPullRefreshView.closeUpPullRefresh();
                    }

                    @Override
                    public void onNext(GetPassengersResponse getPassengersResponse) {
                        super.onNext(getPassengersResponse);
                        List<ZMPassenger> passengers = getPassengersResponse.getAddedPassengers();
                        LOG.e(TAG, "getAddedPassengers=" + passengers.toString());
                        boolean succ = getPassengersResponse.isSucc();
                        if (succ && passengers != null && passengers.size() > 0) {
                            mUpPullRefreshView.renderDataForUpPullOnSuccess(passengers);
                        } else {
                            upPullRefreshFailure();
                            mUpPullRefreshView.noMoreUpPullData();
                        }
                    }
                });
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onDestroy() {
        mGetPassengersUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof PassengerMgmtView) {
            mPassengerMgmtView = (PassengerMgmtView) view;
        }
        if (view instanceof UpPullRefreshView) {
            mUpPullRefreshView = (UpPullRefreshView<ZMPassenger>) view;
        }
    }

    /**
     * SwipeReFreshLayout刷新监听
     */
    public class OnPassengerRefreshListener implements SwipeRefreshLayout.OnRefreshListener {

        @Override
        public void onRefresh() {
            LOG.e(TAG, "开启网络请求");
            loadDownRefreshData();
        }
    }


}
